/*
 * table_CgTypeRelation_t.c
 *
 *  Created on: 2012/04/01
 *      Author: guanxiaoying
 */
#include "../../parser/coredef.h"
typedef struct _CGTYPERELATION
{
	AST			astCcType;		/* 2 */
	AST         astLinkType;
	RBTREE			rbtree;			/* 3 */
} CGTYPERELATION, *PCGTYPERELATION;
#define TableSize  (sizeof(CGTYPERELATION))
#define TableType  PCGTYPERELATION
#define stTable stCgTypeRelationManager.memoryBlocks
#define maxTable stCgTypeRelationManager.maxRecordNum
#define  maxNumPerBlk  (stCgTypeRelationManager.astMaxNum_Block)

/* private field */
static AST  gblTypeRelationCtrl=0;
static TABLEMANAGER stCgTypeRelationManager = {
//		U4                  usIdx;
		0,
//		PSZ				    con_pszName;    constant
		__FILE__,
//		U2					con_usSize;		constant
		TableSize,
//		U2					block_num;	　　　　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_Block;     １回設定のみ
		0,
//		S4					astTabNum_LastPage; 　　　　Reset clear  際　要
		0,
//		S4					astMaxNum_LastPage;     Reset clear  際　要
		0,
//		U4					ulMaxByte;	　　　　　　　　　　クリアしない
		0,
//		U4					con_minPageSize;　　　　　　　　　　constant
		0
};

static VOID resetListener( U4  procid, U2 index )
{
	if( procid == stCgTypeRelationManager.u4Idx )
	{
		if( index > 0 && index < MAX_VOLUMN_OF_TABLE )
		{
			stTable[ index-1] = NULL;
		}
	}
}
static TableType core_getTable( AST index )
{
	TableType temp = (TableType)Check_Com( &stCgTypeRelationManager,  resetListener, index );
	if( temp != NULL ){ return &temp[index%maxNumPerBlk]; } return NULL;
}
static BOOL ClearTable( VOID )
{
	gblTypeRelationCtrl = 0;
	return Clear_Com( &stCgTypeRelationManager );
}
static BOOL ResetTable( VOID )
{
	gblTypeRelationCtrl = 0;
	return Reset_Com( &stCgTypeRelationManager );
}
static BOOL AllocTable( AST astReqArg )
{
	return AllocTable_Com( &stCgTypeRelationManager, astReqArg, resetListener );
}
static AST getMaxLineNum( VOID )
{
	return maxTable;
}
static AST getLeft( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild;
}
static AST setLeft( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astLeftChild = value;
}
static AST getRight( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild;
}
static AST setRight( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astRightChild = value;
}
static AST getParent( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent;
}
static AST setParent( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.astParent = value;
}
static U2 getColor( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor;
}
static U2 setColor( AST index, U2 value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->rbtree.lColor = value;
}

/* give up the way that defining get/set function pointer in the struct,
 * because of memory volume.
 */
static AST  getCCType( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astCcType;
}
static AST  setCCType( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astCcType = value;
}
static AST  getLinkedType( AST index )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astLinkType;
}
static AST  setLinkedType( AST index, AST value )
{
	TableType pTbl = core_getTable( index );
	if( pTbl == NULL )
	{
		return 0;
	}
	return pTbl->astLinkType = value;
}
static ACCESS_RBTREE stAccessRbtree =
{
		AllocTable,
		getMaxLineNum,
		getCCType,
		{
				getLeft,
				setLeft
		},
		{
				getRight,
				setRight
		},
		{
				getParent,
				setParent
		},
		{
				getColor,
				setColor
		}
};
static S4  cmpCcType( AST astCcType, VOID * pastCcType )
{
	if( astCcType == *(AST*)pastCcType )
	{
		return RBT_FOUND;
	}
	if( astCcType > *(AST*)pastCcType )
	{
		return RBT_LESS;
	}
	return RBT_LARGER;
}
static BOOL   fincCcType( AST astCcType, AST * pastCcType )
{
	AST astResult;
	S4 ret;
	ret = FindData( &stAccessRbtree, gblTypeRelationCtrl, &astCcType, &astResult, cmpCcType );
	if( pastCcType != NULL )
	{
		*pastCcType = astResult;
	}
	if( ret == RBT_FOUND )
	{
		return TRUE;
	}
	return FALSE;
}
static AST   addCcType( AST astCcType )
{
	AST astResult;
	S4 find_result;
	AST nMaxIndex=0;
	BOOL bret;

	find_result = FindData( &stAccessRbtree, gblTypeRelationCtrl, &astCcType, &astResult, cmpCcType );
	if( find_result == RBT_FOUND )
	{
		setCCType( astResult, astCcType );
		return astResult;
	}
	bret =  InsertData( &stAccessRbtree, find_result, astResult, &gblTypeRelationCtrl, &nMaxIndex );
	if( bret == FALSE  )
	{
		return (0);
	}
	setCCType( nMaxIndex, astCcType );
	return nMaxIndex;
}
const CGTYPERELATION_ACCESS stCgTypeRelationAccess = {
		ClearTable,
		ResetTable,
		AllocTable,
		getMaxLineNum,
		fincCcType,
		addCcType,
		{
				getLinkedType,
				setLinkedType
		}
};

